package com.gable.exhibition.util;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.CheckExeclDao;
import com.gable.exhibition.view.ImportExcelHospitalModel;
import com.gable.exhibition.view.ImportExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Lesson on 2018/2/8.
 */
@Component
public class CheckImportExcelHospital {

    @Autowired
    CheckExeclDao checkExeclDao;

    public static String [] properties = new String [] {
            "hospitalName","hospitalCode","province","city","area","address","hospitalContacts","hospitalPhone","remark"};

    //用户id作为key
    public static Map<Long,Object> resultMap = new ConcurrentHashMap<Long,Object>();



    public ImportExcelView checkHospital(List<ImportExcelHospitalModel> list ){

        //给到客户端的数据集
        ImportExcelView iev = new ImportExcelView();

        //错误信息
        List<String> errorMessage = new ArrayList<String>();

        int count=0;

        Map map = new HashMap();

        if(list == null || list.size()==0){
            return null;
        }

        Map<String,Integer> hospitalCodeMap = new HashMap<String,Integer>();

        for(int i = 0;i < list.size();i++){
            int row =i+1;

            if(StringUtil.isEmpty(list.get(i).getHospitalCode())){
                errorMessage.add("第"+row+"行，第"+queryProperties("hospitalCode")+"列的医院编号不能为空");
                count++;
            }else{
                int result = checkExeclDao.checkHospitalCode(list.get(i).getHospitalCode());
                if(result > 0){
                    errorMessage.add("第"+row+"行，第"+queryProperties("hospitalCode")+"列的医院编号已存在");
                    count++;
                }
                if(hospitalCodeMap.containsKey(list.get(i).getHospitalCode())){
                    errorMessage.add("第"+row+"行，第"+queryProperties("hospitalCode")+"列的医院编号和"+"第"+hospitalCodeMap.get(list.get(i).getHospitalCode())+"行，"+"第"+queryProperties("hospitalCode")+"列的医院编号相同");
                    count++;
                }
                hospitalCodeMap.put(list.get(i).getHospitalCode(),row);
            }

            Long  provinceId = null;
            if(!StringUtil.isEmpty(list.get(i).getProvince())){
                List<Long> ids = checkExeclDao.checkProvince(list.get(i).getProvince());
                if(ids == null || ids.size() == 0){
                    errorMessage.add("第" + row + "行，第" + queryProperties("province") + "列的省份信息不存在");
                    count++;
                }else if(ids.size() > 1){
                    errorMessage.add("第" + row + "行，第" + queryProperties("province") + "列的省份信息有"+ids.size() +"个");
                    count++;
                }else {
                    list.get(i).setProvinceId(ids.get(0));
                    provinceId = ids.get(0);
                }
            }

            Long cityId = null;
            if(!StringUtil.isEmpty(list.get(i).getCity())){
                map.put("provinceId",provinceId);
                map.put("city",list.get(i).getCity());
                List<Long> id = checkExeclDao.checkCity(map);
                if(id == null || id.size() == 0){
                    errorMessage.add("第" + row + "行，第" + queryProperties("city") + "列的城市信息不存在");
                    count++;
                }else if(id.size() > 1){
                    errorMessage.add("第" + row + "行，第" + queryProperties("city") + "列的城市信息有"+id.size() +"个");
                    count++;
                }else {
                    list.get(i).setCityId(id.get(0));
                    cityId=id.get(0);
                }
            }

            if(!StringUtil.isEmpty(list.get(i).getArea())){
                map.put("cityId",cityId);
                map.put("area",list.get(i).getArea());
                List<Long> areaId = checkExeclDao.checkArea(map);
                if(areaId == null || areaId.size() == 0){
                    errorMessage.add("第" + row + "行，第" + queryProperties("area") + "列的区域信息不存在");
                    count++;
                }else if(areaId.size() > 1){
                    errorMessage.add("第" + row + "行，第" + queryProperties("area") + "列的区域信息有"+areaId.size() +"个");
                    count++;
                }else {
                    list.get(i).setAreaId(areaId.get(0));
                }
            }
        }

        if(count == 0){
            iev.setBool(true);
        }else{
            iev.setBool(false);
        }

        iev.setErrorMessage(errorMessage);
        return iev;
    }


    //获取Properties数组字符串的下标
    public int queryProperties(String str){
        if(str == null || str.equals("")){
            return 0;
        }
        for(int i = 0;i<properties.length;i++){
            if(properties[i].equals(str)){
                int index = i+2;
                return index;
            }
        }
        return 0;
    }

    //正则表达式
    public boolean  CheckPattern(String reg,String str){
        str = str .replaceAll("/","-");
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);
        boolean bl = matcher.matches();
        return bl;
    }

    //根据Map中的key删除map中的元素
    public static synchronized void removeMap (Map map,Long delKey){
        if(map == null || map.size()==0){
            return;
        }
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Long key = Long.parseLong(iterator.next().toString());
            if (key.longValue() == delKey.longValue()) {
                map.remove(key);
                break;
            }
        }
    }

}
